package com.contexticon.mediaTest;

import java.awt.BorderLayout;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.*;
import javax.swing.filechooser.*;

import uk.co.caprica.vlcj.player.MediaPlayer;



public class MovieChooser extends JFrame {

	MediaPlayer mediaPlayer = null;
	
	private JPanel contentPane;
	private JTextField moviePath1;
	private JTextField moviePath2;
	private JTextField min_movie1;
	private JTextField ms_movie1;
	private JTextField ms_movie2;
	private JTextField min_movie2;
	JButton btnNewButton;
	JFileChooser fc;
	private JButton btnDvd;
	private JTextField min_dvd;
	private JTextField ms_dvd;
	private final Action action = new SwingAction();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MovieChooser frame = new MovieChooser();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public MovieChooser() {
		
		fc = new JFileChooser();
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 550, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		moviePath1 = new JTextField();
		moviePath1.setText("/Users/dglodzik/Movies/chloe/chloe.avi");
		moviePath1.setColumns(10);
		
		moviePath2 = new JTextField();
		moviePath2.setText("choose a movie");
		moviePath2.setColumns(10);
		
		btnNewButton = new JButton("Open movie");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
	            int returnVal = fc.showOpenDialog(MovieChooser.this);
	            
	            if (returnVal == JFileChooser.APPROVE_OPTION) {
	            	moviePath1.setText(fc.getSelectedFile().getPath());
	            	
	            //    mov = fc.getSelectedFile();
	            } else {
	           }
	            
				
			//}
			}
		});
		
		JButton btnNewButton_1 = new JButton("Open movie");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
	            int returnVal = fc.showOpenDialog(MovieChooser.this);
	            
	            if (returnVal == JFileChooser.APPROVE_OPTION) {
	            	moviePath2.setText(fc.getSelectedFile().getPath());
	            	
	            //    mov = fc.getSelectedFile();
	            } else {
	           }
			}
		});
		
		JLabel lblMinute = new JLabel("minute");
		
		JLabel lblSecond = new JLabel("ms");
		
		min_movie1 = new JTextField("0");
		min_movie1.setColumns(10);
		
		ms_movie1 = new JTextField("0");
		ms_movie1.setColumns(10);
		
		ms_movie2 = new JTextField();
		ms_movie2.setColumns(10);
		
		min_movie2 = new JTextField();
		min_movie2.setColumns(10);
		
		JButton plaButton1 = new JButton("Play!");
		plaButton1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				if(mediaPlayer!=null) {
					
					int movie1start = Integer.parseInt(min_movie1.getText());
					int movie1offset = Integer.parseInt(ms_movie1.getText());
					long movie1shift = movie1start*60*1000 + movie1offset;
					
					mediaPlayer.playMedia(moviePath1.getText());
					mediaPlayer.skip(movie1shift);
					
					System.out.println(mediaPlayer.getTrackInfo());
					
				}
			}
		});
		
		JButton plaButton2 = new JButton("Play!");
		plaButton2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			
				if(mediaPlayer!=null) {
					
					int movie1start = Integer.parseInt(min_movie2.getText());
					int movie1offset = Integer.parseInt(ms_movie2.getText());
					long movie1shift = movie1start*60*1000 + movie1offset;
					
					mediaPlayer.playMedia(moviePath2.getText());
					mediaPlayer.skip(movie1shift);
					
					System.out.println(mediaPlayer.getTrackInfo());
					
				}

			}
		});
		
		btnDvd = new JButton("DVD!");
		btnDvd.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				//mediaPlayer.playMedia("dvd:///dev/dvd") 
				mediaPlayer.playMedia("dvdsimple:///dev/dvd");
				
			}
		});
		btnDvd.setAction(action);
		
		min_dvd = new JTextField();
		min_dvd.setColumns(10);
		
		ms_dvd = new JTextField();
		ms_dvd.setColumns(10);
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addComponent(btnNewButton)
						.addComponent(btnNewButton_1))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING, false)
						.addComponent(moviePath2, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 105, Short.MAX_VALUE)
						.addComponent(moviePath1, Alignment.TRAILING, 0, 0, Short.MAX_VALUE))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addComponent(lblMinute)
							.addPreferredGap(ComponentPlacement.RELATED, 7, Short.MAX_VALUE)
							.addComponent(lblSecond)
							.addGap(228))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
								.addGroup(gl_contentPane.createSequentialGroup()
									.addComponent(min_movie1, GroupLayout.PREFERRED_SIZE, 58, GroupLayout.PREFERRED_SIZE)
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(ms_movie1, GroupLayout.PREFERRED_SIZE, 58, GroupLayout.PREFERRED_SIZE)
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(plaButton1))
								.addGroup(gl_contentPane.createSequentialGroup()
									.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
										.addGroup(gl_contentPane.createSequentialGroup()
											.addComponent(min_movie2, GroupLayout.PREFERRED_SIZE, 58, GroupLayout.PREFERRED_SIZE)
											.addPreferredGap(ComponentPlacement.RELATED)
											.addComponent(ms_movie2, GroupLayout.PREFERRED_SIZE, 58, GroupLayout.PREFERRED_SIZE))
										.addGroup(gl_contentPane.createSequentialGroup()
											.addComponent(min_dvd, GroupLayout.PREFERRED_SIZE, 58, GroupLayout.PREFERRED_SIZE)
											.addGap(6)
											.addComponent(ms_dvd, GroupLayout.PREFERRED_SIZE, 58, GroupLayout.PREFERRED_SIZE)))
									.addPreferredGap(ComponentPlacement.RELATED)
									.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING, false)
										.addComponent(btnDvd, 0, 0, Short.MAX_VALUE)
										.addComponent(plaButton2, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
							.addContainerGap(95, Short.MAX_VALUE))))
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGap(21)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblSecond)
						.addComponent(lblMinute))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
						.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
							.addComponent(min_movie1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addComponent(ms_movie1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addComponent(plaButton1))
						.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
							.addComponent(btnNewButton)
							.addComponent(moviePath1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
					.addGap(34)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
							.addComponent(moviePath2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addComponent(btnNewButton_1))
						.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
							.addComponent(min_movie2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addComponent(ms_movie2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addComponent(plaButton2)))
					.addGap(31)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addComponent(min_dvd, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(ms_dvd, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(btnDvd)))
		);
		contentPane.setLayout(gl_contentPane);
		

	}
	
	void setMediaPlayer(MediaPlayer pl) {
		this.mediaPlayer = pl;
	}
	private class SwingAction extends AbstractAction {
		public SwingAction() {
			putValue(NAME, "SwingAction");
			putValue(SHORT_DESCRIPTION, "Some short description");
		}
		public void actionPerformed(ActionEvent e) {
		}
	}
}
